package com.vinoxm.rss.model;

import com.vinoxm.rss.handler.validate.annotation.ValidateColumn;
import com.vinoxm.rss.handler.validate.annotation.ValidateRule;
import lombok.Data;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@ValidateRule(min = 1)
public class RssSubscribe {

    private Long id;
    @ValidateColumn
    private String name;
    @ValidateColumn("name_jp")
    private String nameJP;
//    @ValidateColumn
    private String season;
    @ValidateColumn("start_time")
    private Date startTime;
    @ValidateColumn
    private String cover;
    @ValidateColumn("is_short")
    private Integer isShort;
    @ValidateColumn("anime_type")
    private Integer animeType;
    @ValidateColumn("origin_type")
    private String originType;
    @ValidateColumn("type_tag")
    private String typeTag;
    @ValidateColumn
    private String staff;
    @ValidateColumn
    private String cast;
    @ValidateColumn
    private String broadcast;

    private String url;
    private String regex;

    @ValidateColumn
    private String fin;
    @ValidateColumn
    private Integer goon;

    private List<RssResult> result;
    @ValidateColumn
    private List<RssLink> link;
    @ValidateColumn
    private List<RssCopyright> copyright;

    private Integer count;
    private Date lastPub;
    private Boolean lazy;

    public RssSubscribe() {
        lazy = false;
    }

    public void replaceDetail(RssSubscribe rss) {
        this.name = rss.getName();
        this.nameJP = rss.getNameJP();
        this.startTime = rss.getStartTime();
        this.cover = rss.getCover();
        this.isShort = rss.getIsShort();
        this.animeType = rss.getAnimeType();
        this.originType = rss.getOriginType();
        this.typeTag = rss.getTypeTag();
        this.staff = rss.getStaff();
        this.cast = rss.getCast();
        this.broadcast = rss.getBroadcast();
    }

    public List<RssLink> getLinkWithPid() {
        return this.link == null ? Collections.emptyList() : this.link.stream().peek(r -> r.setPid(this.id)).collect(Collectors.toList());
    }

    public List<RssCopyright> getCopyrightWithPid() {
        return this.copyright == null ? Collections.emptyList() : this.copyright.stream().peek(r -> r.setPid(this.id)).collect(Collectors.toList());
    }

    /**
     public void setLinkWithPid(List<RssLink> link, Long pid) {
        this.link = link;
        if (this.link != null && this.link.size() > 0) {
            for (RssDetail rssLink : this.link) {
                rssLink.setPid(pid);
            }
        }
    }

    public void setCopyrightWithPid(List<RssCopyright> copyright, Long pid) {
        this.copyright = copyright;
        if (this.copyright != null && this.copyright.size() > 0) {
            for (RssDetail rssCopyright : this.copyright) {
                rssCopyright.setPid(pid);
            }
        }
    }

    public void setDetailPid(Long pid) {
        if (this.copyright != null && this.copyright.size() > 0) {
            for (RssDetail rssCopyright : this.copyright) {
                rssCopyright.setPid(pid);
            }
        }
        if (this.link != null && this.link.size() > 0) {
            for (RssDetail rssLink : this.link) {
                rssLink.setPid(pid);
            }
        }
    }
    */

    @Override
    public String toString() {
        return "RssSubscribe{" +
                "id=" + id +
                ", name='" + name + '\'' +
//                ", nameJP='" + nameJP + '\'' +
                ", url='" + url + '\'' +
                ", regex='" + regex + '\'' +
                ", season='" + season + '\'' +
                ", startTime=" + startTime +
//                ", cover='" + cover + '\'' +
                ", fin='" + fin + '\'' +
                ", count='" + count + '\'' +
                ", lastPub='" + lastPub + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RssSubscribe that = (RssSubscribe) o;
        return Objects.equals(id, that.id) && Objects.equals(name, that.name) && Objects.equals(season, that.season);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, season);
    }
}
